package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
//添加一个商品到购物车
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        //检测商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());//设置用户id
        System.out.println(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.select(shoppingCart);//因为搜索条件限制，这里一次只能查出一条数据

        //存在则添加数量
        if (list.size() != 0){

            shoppingCartMapper.update(list.get(0).getId());
        }else {//不存在就插入
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            Long dishId = shoppingCart.getDishId();
            if (dishId != null){
                //说明添加了菜品
                DishDTO dishDTO = dishMapper.selectById(dishId);
                shoppingCart.setName(dishDTO.getName());
                shoppingCart.setImage(dishDTO.getImage());
                shoppingCart.setAmount(dishDTO.getPrice());
            }else {
                //说明添加了套餐
                Setmeal setmeal = setmealMapper.selectById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            //插入购物车
            shoppingCartMapper.insert(shoppingCart);
            System.out.println("添加成功");
        }

    }
//查看购物车
    @Override
    public List<ShoppingCart> showCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.select(shoppingCart);
    }
//清空购物车
    @Override
    public void delete() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
//删除购物车商品
    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCartMapper.subOne(shoppingCart);//购物车商品数量-1
        shoppingCartMapper.check(shoppingCart);//-1后检查数量是否为0，是就删除数据
    }
}
